﻿package net.wangit.view.unit.navigator;

import net.wangit.support.PageNavigator;

public class IconPageNavigator implements PageNavigator {

	private static final String ICON_FIRST = "/common/images/list/n0.png";
	private static final String ICON_FIRST_DISABLED = "/common/images/list/n0d.png";
	private static final String ICON_PREVIOUS = "/common/images/list/n4.png";
	private static final String ICON_PREVIOUS_DISABLED = "/common/images/list/n4d.png";
	private static final String ICON_NEXT = "/common/images/list/n6.png";
	private static final String ICON_NEXT_DISABLED = "/common/images/list/n6d.png";
	private static final String ICON_LAST = "/common/images/list/n9.png";
	private static final String ICON_LAST_DISABLED = "/common/images/list/n9d.png";

	public IconPageNavigator() {
	}

	public String getHtml(String domain, String unitId, int dataSize,
			int maxSize, int listSize, int pageNumber, int maxPageNumber) {
		StringBuffer html = new StringBuffer();
		html.append("<span class=\"").append("NT").append("\">").append(
				pageNumber).append("/").append(maxPageNumber).append(
				"&nbsp;&nbsp;").append(dataSize).append("/").append(maxSize)
				.append("</span>");
		html.append("&nbsp;&nbsp;");
		setFirst(html, pageNumber);
		setPrevious(html, pageNumber);
		setNext(html, pageNumber, maxPageNumber);
		setLast(html, pageNumber, maxPageNumber);
		html.append("&nbsp;");
		html.append("<span class=\"").append("NC").append(
				"\"><input type=\"hidden\" name=\"").append(unitId)
				.append(".p").append("\"/><input class=\"").append("NN")
				.append("\"/><input class=\"").append("NG").append(
						"\" type=\"button\" value=\"go\" onclick=\"_go(")
				.append(pageNumber).append(",").append(maxPageNumber).append(
						")\"/></span>");
		return html.toString();
	}

	private void setFirst(StringBuffer html, int pageNumber) {
		if (pageNumber > 1)
			setEnabledIcon(html, 1, IconPageNavigator.ICON_FIRST);
		else
			setDisabledIcon(html, IconPageNavigator.ICON_FIRST_DISABLED);
	}

	private void setPrevious(StringBuffer html, int pageNumber) {
		if (pageNumber > 1) {
			int pageNo = pageNumber - 1 >= 1 ? pageNumber - 1 : 1;
			setEnabledIcon(html, pageNo, IconPageNavigator.ICON_PREVIOUS);
		} else {
			setDisabledIcon(html, IconPageNavigator.ICON_PREVIOUS_DISABLED);
		}
	}

	private void setNext(StringBuffer html, int pageNumber, int maxPageNumber) {
		if (pageNumber < maxPageNumber) {
			int pageNo = pageNumber + 1 <= maxPageNumber ? pageNumber + 1
					: maxPageNumber;
			setEnabledIcon(html, pageNo, IconPageNavigator.ICON_NEXT);
		} else {
			setDisabledIcon(html, IconPageNavigator.ICON_NEXT_DISABLED);
		}
	}

	private void setLast(StringBuffer html, int pageNumber, int maxPageNumber) {
		if (pageNumber < maxPageNumber)
			setEnabledIcon(html, maxPageNumber, IconPageNavigator.ICON_LAST);
		else
			setDisabledIcon(html, IconPageNavigator.ICON_LAST_DISABLED);
	}

	private void setEnabledIcon(StringBuffer html, int pageNumber, String icon) {
		html.append("<img class=\"").append("NE").append("\" onclick=\"_to(")
				.append(pageNumber).append(")\" src=\"").append(icon).append(
						"\"/>");
	}

	private void setDisabledIcon(StringBuffer html, String icon) {
		html.append("<img src=\"").append(icon).append("\"/>");
	}
}
